    var datamodel = new DataModel();
     function UpdateMonthOutgo(currDate)
     {
        var newOutgo = datamodel.getMonthOutgo(currDate.getFullYear(),(currDate.getMonth() + 1));
        var dateStr = currDate.getFullYear().toString() + (currDate.getMonth() + 1).toString();
         $("#outgo" + dateStr).html(newOutgo);
     }
     function UpdateMonthIncome(currDate)
     {
        var newIncome = datamodel.getMonthIncome(currDate.getFullYear(),(currDate.getMonth() + 1));
        var dateStr = currDate.getFullYear().toString() + (currDate.getMonth() + 1).toString();
        $("#income" + dateStr).html(newIncome);
     }
    function hideEditAndSave()
    {
        $("div[id^=turnoveredit]:visible").hide(200);
        var dataindex = $("div[id^=turnoveredit]:visible").attr("data-turnoverindex");
        var oldTurnover = 
                    datamodel.turnovers[dataindex];
        if(oldTurnover)
        {
            oldTurnover.money = $("#turnovereditmoney" + oldTurnover.id).val();
            $("#turnovershowmoney" + oldTurnover.id).html(oldTurnover.money);
            oldTurnover.remark = $("#turnovereditremark" + oldTurnover.id).val();
            $("#turnovershowremark" + oldTurnover.id).html(oldTurnover.remark);
            
            oldTurnover.time = $("#turnovereditdate" + oldTurnover.id).val();
            var currDate = new Date(oldTurnover.time);
            var timeStr = currDate.format("yyyy-MM-dd");
             $("#turnovershowdate" + oldTurnover.id).html(timeStr);
            

            //不是转账的话
            if(oldTurnover.type != 2)
            {
                oldTurnover.catid = $("#turnovereditcatid" + oldTurnover.id).val();
                var imgStr = ".\\imgs\\{0}.png";
                imgStr = imgStr.format(oldTurnover.catid);
                $("#turnovercatimg" + oldTurnover.id).attr("src",imgStr);

                
                var cat = datamodel.getCategory(oldTurnover.catid);
                $("#turnovershowcat" + oldTurnover.id).html(cat.name);
                oldTurnover.catid
                //收入可以更改to
                if(oldTurnover.type == 1)
                {
                     oldTurnover.to = $("#turnovereditaccount" + oldTurnover.id).val();
                     UpdateMonthIncome(currDate)
                     
                    
                } else if(oldTurnover.type == 0)//支出改from
                {
                     oldTurnover.from = $("#turnovereditaccount" + oldTurnover.id).val();
                     UpdateMonthOutgo(currDate  );
                }
            }
            
            oldTurnover.Update();
            //$('ul').listview("refresh");
            //console.log(oldTurnover);
        }
                    

        
        
        
        return $("div[id^=turnoveredit]:visible").attr("data-turnoverid");
        
    }
    $("#pageturnover").live('pageinit', function() {
        InitTurnover();
    });
    function InitTurnover(){
        
        $("#turnoverlist").html("");
           
        datamodel.init();
        
        var dates = new Array();
        var incomes = new Array();
        var outgos = new Array();
        
        var index = 0;
        for(var key in datamodel.turnovers)
        {
            var itemData = datamodel.turnovers[key];
            var currDate = new Date(datamodel.turnovers[key].time);
            //month从0开始
            var dateStr = currDate.getFullYear().toString() + (currDate.getMonth() + 1).toString();
            var listviewIDStr = "listview" + dateStr;
            
            if(!arrayexist(dates,dateStr))
            {
                dates.push(dateStr);
                var collapsed = (index != 0);

                 var monthHTML = 
                 "<div data-role='collapsible' data-collapsed='{0}' data-month='{1}{2}' data-content-theme='d'>"
                  + "<h3 onclick='hideEditAndSave()'>{1}年 {2}月"
                  + "    收：￥<span id=income{3}>0</span>" + "    支：￥<span id=outgo{3}>0</span></h3>"
                  + "<ul data-role='listview' data-divider-theme='b' data-inset=\"false\" id={4}>"
                  + "</ul></div>"
                  monthHTML = 
                  monthHTML.format(collapsed,currDate.getFullYear(),(currDate.getMonth() + 1),dateStr,listviewIDStr);
                 
                  $('#turnoverlist').append(monthHTML);
            }
            
            var typeStr = "流水类型:";
            var typeColor = '';
            var typeMoneyColor = '';
            switch(itemData.type)
            {
                case "0": 
                 {
                    //typeStr += "支出";
                    typeColor = 'b-red';
                    typeMoneyColor = 'red';
                    var now = parseInt($('#outgo' + dateStr).html());
                    now += parseInt(itemData.money);
                    $('#outgo' + dateStr).html(now);
                 }
                 break;
                case "1":
                 {
                    //typeStr += "收入";
                    typeColor = 'b-green';
                    typeMoneyColor = 'green';
                    var now = parseInt($('#income' + dateStr).html());
                    now += parseInt(itemData.money);
                    $('#income' + dateStr).html(now);
                 }
                 break;
                case "2":
                   //typeStr += "转账";
                   typeColor = 'b-blue';
                   typeMoneyColor = 'blue';
                   break;
            }

            var timeStr = currDate.format("yyyy-MM-dd");
            ///////////////////////////////////////////////////////////////////////
            //生成账户选择框
            var accountSelectHTML = "";
            if(itemData.type != 2)
            {
                 var accountsSelect = "";
            for(var accountid in datamodel.accounts)
            {
                var account = datamodel.accounts[accountid];
                var oneSelect = "<option value='{0}'>{1}</option>"
                oneSelect = oneSelect.format(account.id,account.name);
                accountsSelect += oneSelect;
            }
            
            accountSelectHTML = 
            "<div data-role='fieldcontain'><label for='turnovereditaccount{0}'class='select'>账户:</label>"
            + "<select id='turnovereditaccount{0}' data-inline='true'>"
            + accountsSelect
            + "</select></div>";
            accountSelectHTML = accountSelectHTML.format( datamodel.turnovers[key].id);
            }
           ///////////////////////////////////////////////////////////////////////
            //生成类别选择框
            var categorySelectHTML = "";
            if(itemData.type != 2)
            {
                 var categorysSelect = "";
            for(var categoryid in datamodel.categorys)
            {
                var category = datamodel.categorys[categoryid];
                if(category.type == itemData.type)
                {
                    var oneSelect = "<option value='{0}'>{1}</option>"
                    oneSelect = oneSelect.format(category.id,category.name);
                    categorysSelect += oneSelect;
                }
                
            }
                      
            categorySelectHTML = 
            "<div data-role='fieldcontain'><label for='turnovereditcatid{0}' class='select'>类别:</label>"
            + "<select value='' id='turnovereditcatid{0}' name='turnovereditcatid{0}' data-inline='true'>"
            + categorysSelect
            + "</select></div>";
            categorySelectHTML = categorySelectHTML.format( datamodel.turnovers[key].id);
            
            }
           
            
             ///////////////////////////////////////////////////////////////////////
            
            var catImgHTML =  "<img id='turnovercatimg{0}' src='.\\imgs\\{1}.png' height ='60'/>";
            if(itemData.type != 2)
            {
                catImgHTML = catImgHTML.format(itemData.id,itemData.catid);
            } else 
            {
                catImgHTML = catImgHTML.format(itemData.id,"zz");
            }
            
           
             ///////////////////////////////////////////////////////////////////////
            //初始化整个item的html
            ////////////////////////////////////////////////////////////////////////
            //0:流水id,1:流水类别字符串(已经不用),2:金额,3:备注,4:时间字符串,5:流水数组中index,6:类别名称或转账去向
            var itemHTML =  
            "<li class='"+ typeColor +"' data-theme='c' data-turnoverid='{0}' data-turnoverindex='{5}'>"
              + "<a href='#page1' data-transition='slide' >"
              + catImgHTML
              + "<div class='left'> <div id='turnovershowcat{0}'>{6}</div><span id='turnovershowdate{0}'>{4}</span> <span id='turnovershowremark{0}'>{3}</span> </div>"
              + "<span class='money "+ typeMoneyColor +"'>￥<span id ='turnovershowmoney{0}'>{2}</span></span></a>"
              + "</li>"//<li>标签到这里结束是因为后面监听了<li>的click事件
              //编辑框
              + "<div style='display:none' id = 'turnoveredit{0}' data-turnoverid='{0}' data-turnoverindex='{5}'>"
              + "<label for='basic'>金额:</label>" 
              + "<input type='number' name='name' id='turnovereditmoney{0}' value='{2}'  />"
              + accountSelectHTML
              + categorySelectHTML
              + "<div data-role='fieldcontain'>"
              + "<label for='turnovereditdate{0}'>日期:</label>" 
              + "<input type='date' id='turnovereditdate{0}' value='{4}'></input></div>"
              + "<div data-role='fieldcontain'>"
              + "<textarea id='turnovereditremark{0}' placeholder='请填写备注信息'>{3}</textarea></div>"
              + "</div>";
              
  
              var cat = datamodel.getCategory(itemData.catid);
              var leftMainStr = ""
              if(cat)
              {
                 leftMainStr = cat.name;
              } 
              if(itemData.type == 2)
              {
                
                leftMainStr = "{0}-->{1}";
                var accountFrom = datamodel.getAccount(itemData.from);
                var accountTo = datamodel.getAccount(itemData.to);
                leftMainStr = leftMainStr.format(accountFrom.name,accountTo.name);
              }
             
              itemHTML = 
              itemHTML.format(itemData.id, typeStr,itemData.money,itemData.remark,
              timeStr,key,leftMainStr);
              
              $('#' +　listviewIDStr).append(itemHTML);
              
            ++ index;
             
        }
        

        $('#turnoverlist').collapsibleset('refresh')
        $('ul[data-role=\'listview\']').listview();
        $('select').selectmenu();
        
        $('li').click(function(){
            var hideid = hideEditAndSave();
            if(hideid != $(this).attr("data-turnoverid")){
              $("#turnoveredit" + $(this).attr("data-turnoverid")).show(200);
              
              var turnover = datamodel.turnovers[$(this).attr("data-turnoverindex")];
              var acc;
              if(turnover.type == 0)
              {
                acc = turnover.from;
              } else if(turnover.type == 1) 
              {
                acc = turnover.to;
              }
              
               $("#turnovereditaccount" + turnover.id).val(acc).selectmenu("refresh");
               $("#turnovereditcatid" + turnover.id).val(turnover.catid).selectmenu("refresh");
             }
        });
    }